import yfinance as yf
import pandas as pd
import plotly.express as px
data = yf.download("AAPL", start="2012-01-01" ,end = "2023-05-01", interval="1mo")
[*********************100%***********************] 1 of 1 completed
data['Returns'] = data['Adj Close'].pct_change()
data
| Open | High | Low | Close | Adj Close | Volume | Returns | |
|---|---|---|---|---|---|---|---|
| Date | |||||||
| 2012-01-01 | 14.621429 | 16.365713 | 14.607143 | 16.302856 | 13.856495 | 6859854400 | NaN |
| 2012-02-01 | 16.371786 | 19.557501 | 16.213572 | 19.372856 | 16.465820 | 11368554400 | 0.188311 |
| 2012-03-01 | 19.577499 | 22.194643 | 18.436428 | 21.412500 | 18.199396 | 15584338000 | 0.105283 |
| 2012-04-01 | 21.493929 | 23.000000 | 19.821428 | 20.856428 | 17.726772 | 15598990400 | -0.025969 |
| 2012-05-01 | 20.889286 | 21.312857 | 18.649286 | 20.633215 | 17.537058 | 11106345600 | -0.010702 |
| ... | ... | ... | ... | ... | ... | ... | ... |
| 2022-12-01 | 148.210007 | 150.919998 | 125.870003 | 129.929993 | 129.552719 | 1675731200 | -0.120816 |
| 2023-01-01 | 130.279999 | 147.229996 | 124.169998 | 144.289993 | 143.871017 | 1443652500 | 0.110521 |
| 2023-02-01 | 143.970001 | 157.380005 | 141.320007 | 147.410004 | 146.981964 | 1307198900 | 0.021623 |
| 2023-03-01 | 146.830002 | 165.000000 | 143.899994 | 164.899994 | 164.672226 | 1520266600 | 0.120357 |
| 2023-04-01 | 164.270004 | 169.850006 | 159.779999 | 169.679993 | 169.445618 | 969709700 | 0.028987 |
136 rows × 7 columns
# resample the data
monthly = data.Returns.resample("M").sum()
toHeatMap = pd.DataFrame(monthly)
toHeatMap["Month"] = toHeatMap.index.month
toHeatMap["Year"] = toHeatMap.index.year
Show = toHeatMap.groupby(by=['Month', 'Year']).sum().unstack()
Show.columns = Show.columns.droplevel(0)
Show.rename(columns={1: "January", 2: "February", 3: "March", 4: "April", 5: "May", 6: "June",
7: "July", 8: "August", 9: "September", 10: "October", 11: "November", 12: "December"},
inplace=True)
Show
| Year | 2012 | 2013 | 2014 | 2015 | 2016 | 2017 | 2018 | 2019 | 2020 | 2021 | 2022 | 2023 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Month | ||||||||||||
| 1 | 0.000000 | -0.144090 | -0.107696 | 0.061424 | -0.075242 | 0.047746 | -0.010636 | 0.055154 | 0.054010 | -0.005502 | -0.015712 | 0.110521 |
| 2 | 0.188311 | -0.030934 | 0.051219 | 0.096449 | -0.006678 | 0.128883 | 0.063848 | 0.040315 | -0.116798 | -0.081085 | -0.055269 | 0.021623 |
| 3 | 0.105283 | 0.008699 | 0.026058 | -0.027549 | 0.133327 | 0.053237 | -0.054210 | 0.101731 | -0.067554 | 0.008845 | 0.058821 | 0.120357 |
| 4 | -0.025969 | 0.000271 | 0.099396 | 0.005787 | -0.139921 | -0.000070 | -0.015020 | 0.056436 | 0.155374 | 0.076218 | -0.097131 | 0.028987 |
| 5 | -0.010702 | 0.015696 | 0.072718 | 0.040991 | 0.065287 | 0.063418 | 0.130764 | -0.127573 | 0.082165 | -0.052107 | -0.055883 | NaN |
| 6 | 0.010853 | -0.112457 | 0.033401 | -0.033206 | -0.036831 | -0.053323 | -0.005598 | 0.134873 | 0.150492 | 0.100976 | -0.080080 | NaN |
| 7 | 0.045822 | 0.141225 | 0.028731 | -0.032927 | 0.090063 | 0.032704 | 0.027983 | 0.076394 | 0.165132 | 0.064982 | 0.188634 | NaN |
| 8 | 0.089200 | 0.076658 | 0.072176 | -0.070404 | 0.018136 | 0.102669 | 0.196227 | -0.020184 | 0.214380 | 0.040930 | -0.032552 | NaN |
| 9 | 0.007102 | -0.015029 | -0.012184 | -0.017389 | 0.071276 | -0.056553 | -0.004825 | 0.077038 | -0.100908 | -0.066640 | -0.119756 | NaN |
| 10 | -0.107600 | 0.096382 | 0.071960 | 0.083409 | 0.004334 | 0.096808 | -0.030478 | 0.110684 | -0.060012 | 0.058657 | 0.109551 | NaN |
| 11 | -0.016865 | 0.063842 | 0.101204 | -0.010042 | -0.026598 | 0.016623 | -0.184045 | 0.074329 | 0.093606 | 0.103471 | -0.034629 | NaN |
| 12 | -0.086590 | 0.014792 | -0.067866 | -0.106419 | 0.053335 | -0.011706 | -0.113616 | 0.102083 | 0.116497 | 0.075797 | -0.120816 | NaN |
# plot the data
fig = px.line(Show)
fig.update_layout(xaxis_title="Month", yaxis_title="Returns",height=700,width=1000)
fig.show()
There's a cyclic trend between the 9th month and the 10th month (iphone release: september/october)
Years not correlated:
df = pd.read_html('https://en.wikipedia.org/wiki/IPhone')[3]
df
| model | release(d) | discontinued | support | |||||
|---|---|---|---|---|---|---|---|---|
| model | with OS | date | discontinued | ended | final OS[a] | lifespan[b] | ||
| model | with OS | date | discontinued | ended | final OS[a] | max[c] | min[d] | |
| 0 | iPhone | iPhone OS 1.0 | June 29, 2007 | June 9, 2008 | June 20, 2010 | iPhone OS 3.1.3 | 2 years, 11 months | 2 years |
| 1 | iPhone 3G | iPhone OS 2.0 | July 11, 2008 | August 9, 2010 | March 3, 2011 | iOS 4.2.1 | 2 years, 7 months | 6 months |
| 2 | iPhone 3GS | iPhone OS 3.0 | June 19, 2009 | September 12, 2012 | September 18, 2013 (late, single update: Febru... | iOS 6.1.3 (6.1.6) | 4 years, 2 months | 1 year |
| 3 | iPhone 4 | iOS 4.0 | June 24, 2010 | September 10, 2013 | September 17, 2014 | iOS 7.1.2 | 4 years, 2 months | 1 year |
| 4 | iPhone 4S | iOS 5.0 | October 14, 2011 | September 9, 2014 | September 12, 2016 (late, single update: July ... | iOS 9.3.5 (9.3.6) | 4 years, 10 months | 2 years |
| 5 | iPhone 5 | iOS 6.0 | September 21, 2012 | September 10, 2013 | September 18, 2017 (late, single update: July ... | iOS 10.3.3 (10.3.4) | 4 years, 11 months | 4 years |
| 6 | iPhone 5C | iOS 7.0 | September 20, 2013 | September 9, 2015 | September 18, 2017 | iOS 10.3.3 | 3 years, 11 months | 2 years |
| 7 | iPhone 5S | iOS 7.0 | September 20, 2013 | March 21, 2016 | September 18, 2019 (last security update: Janu... | iOS 12.4.1 (12.5.7) | 5 years, 11 months | 3 years, 5 months |
| 8 | iPhone 6 / 6 Plus | iOS 8.0 | September 19, 2014 | September 7, 2016 | September 18, 2019 (last security update: Janu... | iOS 12.4.1 (12.5.7) | 4 years, 11 months | 3 years |
| 9 | iPhone 6S / 6S Plus | iOS 9.0 | September 25, 2015 | September 12, 2018 | August 17, 2022 still supported (latest securi... | iOS 15.6.1 (15.7.6) | 7 years, 7 months | 4 years, 8 months |
| 10 | iPhone SE (1st) | iOS 9.3 | March 31, 2016 | September 12, 2018 | August 17, 2022 still supported (latest securi... | iOS 15.6.1 (15.7.6) | 7 years, 1 month | 4 years, 8 months |
| 11 | iPhone 7 / 7 Plus | iOS 10.0 | September 16, 2016 | September 10, 2019 | August 17, 2022 still supported (latest securi... | iOS 15.6.1 (15.7.6) | 6 years, 8 months | 3 years, 8 months |
| 12 | iPhone 8 / 8 Plus | iOS 11.0 | September 22, 2017 | April 15, 2020 | current | latest iOS | 5 years, 9 months | 3 years, 2 months |
| 13 | iPhone X | iOS 11.0.1 | November 3, 2017 | September 12, 2018 | current | latest iOS | 5 years, 7 months | 4 years, 9 months |
| 14 | iPhone XR | iOS 12.0 | October 26, 2018 | September 14, 2021 | current | latest iOS | 4 years, 8 months | 1 year, 9 months |
| 15 | iPhone XS / XS Max | iOS 12.0 | September 21, 2018 | September 10, 2019 | current | latest iOS | 4 years, 9 months | 3 years, 9 months |
| 16 | iPhone 11 | iOS 13.0 | September 20, 2019 | September 7, 2022 | current | latest iOS | 3 years, 9 months | 9 months |
| 17 | iPhone 11 Pro / 11 Pro Max | iOS 13.0 | September 20, 2019 | October 13, 2020 | current | latest iOS | 3 years, 9 months | 2 years, 8 months |
| 18 | iPhone SE (2nd) | iOS 13.4 | April 24, 2020 | March 8, 2022 | current | latest iOS | 3 years, 2 months | 1 year, 3 months |
| 19 | iPhone 12 | iOS 14.1 | October 23, 2020 | NaN | current | latest iOS | 2 years, 8 months | NaN |
| 20 | iPhone 12 Mini | iOS 14.2 | November 13, 2020 | September 7, 2022 | current | latest iOS | 2 years, 7 months | 9 months |
| 21 | iPhone 12 Pro / 12 Pro Max | iOS 14.1 (12 Pro) iOS 14.2 (12 Pro Max) | October 23, 2020 November 13, 2020 | September 14, 2021 | current | latest iOS | 2 years, 8 months (12 Pro) 2 years, 7 months (... | 1 year, 9 months |
| 22 | iPhone 13 / 13 Mini | iOS 15.0 | September 24, 2021 | NaN | current | latest iOS | 1 year, 9 months | NaN |
| 23 | iPhone 13 Pro / 13 Pro Max | iOS 15.0 | September 24, 2021 | September 7, 2022 | current | latest iOS | 1 year, 9 months | 9 months |
| 24 | iPhone SE (3rd) | iOS 15.4 | March 18, 2022 | NaN | current | latest iOS | 1 year, 3 months | NaN |
| 25 | iPhone 14 / 14 Plus | iOS 16.0 | September 16, 2022 October 7, 2022 | NaN | current | latest iOS | 9 months | NaN |
| 26 | iPhone 14 Pro / 14 Pro Max | iOS 16.0 | September 16, 2022 | NaN | current | latest iOS | 9 months | NaN |
| 27 | Legend: Discontinued and unsupported Discont... | Legend: Discontinued and unsupported Discont... | Legend: Discontinued and unsupported Discont... | Legend: Discontinued and unsupported Discont... | Legend: Discontinued and unsupported Discont... | Legend: Discontinued and unsupported Discont... | Legend: Discontinued and unsupported Discont... | Legend: Discontinued and unsupported Discont... |
| 28 | Legend: | Discontinued and unsupported | Discontinued, bug fixes only | Discontinued, still supported | Current or still sold | NaN | NaN | NaN |
| 29 | Remarks: .mw-parser-output .reflist{font-size:... | Remarks: .mw-parser-output .reflist{font-size:... | Remarks: .mw-parser-output .reflist{font-size:... | Remarks: .mw-parser-output .reflist{font-size:... | Remarks: .mw-parser-output .reflist{font-size:... | Remarks: .mw-parser-output .reflist{font-size:... | Remarks: .mw-parser-output .reflist{font-size:... | Remarks: .mw-parser-output .reflist{font-size:... |
| 30 | Remarks: | .mw-parser-output .reflist{font-size:90%;margi... | NaN | NaN | NaN | NaN | NaN | NaN |
data['Month']= data.index.month
signal_series = data[(data['Month'] == 10)]
signal_series
| Open | High | Low | Close | Adj Close | Volume | Returns | Month | |
|---|---|---|---|---|---|---|---|---|
| Date | ||||||||
| 2012-10-01 | 23.969999 | 24.169643 | 20.989286 | 21.261429 | 18.148588 | 12142830000 | -0.107600 | 10 |
| 2013-10-01 | 17.087500 | 19.258928 | 17.081429 | 18.667856 | 16.314390 | 7837732000 | 0.096382 | 10 |
| 2014-10-01 | 25.147499 | 27.010000 | 23.795000 | 27.000000 | 24.128706 | 5441120800 | 0.071960 | 10 |
| 2015-10-01 | 27.267500 | 30.305000 | 26.827499 | 29.875000 | 27.154360 | 4450048400 | 0.083409 | 10 |
| 2016-10-01 | 28.177500 | 29.672501 | 28.070000 | 28.385000 | 26.351681 | 2747657200 | 0.004334 | 10 |
| 2017-10-01 | 38.564999 | 42.412498 | 38.115002 | 42.259998 | 39.924809 | 2017165200 | 0.096808 | 10 |
| 2018-10-01 | 56.987499 | 58.367500 | 51.522499 | 54.715000 | 52.473160 | 3158994000 | -0.030478 | 10 |
| 2019-10-01 | 56.267502 | 62.437500 | 53.782501 | 62.189999 | 60.567390 | 2433210800 | 0.110684 | 10 |
| 2020-10-01 | 117.639999 | 125.389999 | 107.720001 | 108.860001 | 107.071648 | 2894666500 | -0.060012 | 10 |
| 2021-10-01 | 141.899994 | 153.169998 | 138.270004 | 149.800003 | 148.286743 | 1565079200 | 0.058657 | 10 |
| 2022-10-01 | 138.210007 | 157.500000 | 134.369995 | 153.339996 | 152.641541 | 1868139700 | 0.109551 | 10 |
money = 10000
returns1 = signal_series['Returns'] * money
equity = returns1.cumsum() + money
equity
Date 2012-10-01 8924.000474 2013-10-01 9887.817697 2014-10-01 10607.416076 2015-10-01 11441.506094 2016-10-01 11484.849314 2017-10-01 12452.925822 2018-10-01 12148.149349 2019-10-01 13254.994205 2020-10-01 12654.874768 2021-10-01 13241.448461 2022-10-01 14336.962791 Name: Returns, dtype: float64
# plot the Equity
fig = px.line(equity)
fig.update_layout(xaxis_title="Month", yaxis_title="Returns",height=600,width=1000)
fig.show()